package com.egene.zh.activity.member;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.egene.zh.activity.wiki.WikInfoActivity;
import com.egene.zh.activity.wiki.WikInfoActivity_;
import com.egene.zh.api.EgeneApi;
import com.egene.zh.base.BaseRecycleViewFragment;
import com.egene.zh.base.RecycleBaseAdapter;
import com.egene.zh.dao.UserDao;
import com.egene.zh.model.HealthItem;
import com.egene.zh.model.HealthList;
import com.egene.zh.model.HealthListItem;
import com.egene.zh.model.net.ResponseList;
import com.egene.zh.utils.JsonUtil;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

@EFragment
public class HealthListFragment extends BaseRecycleViewFragment {

    public static final String TYPE = "TYPE";
    private String type;

    @Bean
    UserDao userDao;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if(bundle != null){
            type = bundle.getString(TYPE);
        }

    }

    @Override
    protected boolean isNeedListDivider() {
        return true;
    }

    @Override
    protected RecycleBaseAdapter getListAdapter() {
        return new HealthRecycleAdapter();
    }


    @Override
    protected void sendRequestData() {
        fetchList();
    }

    void fetchList() {
        if("4".equals(type)){
            EgeneApi.getPersonalProjectList(userDao.getUser().getUserID(), new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    HealthList result = JsonUtil.parse(responseBody, HealthList.class);
                    if(result != null){
                        if(result.isSuccess()){
                            List<HealthListItem> l = tr(result.getPersoncareplan());
                            updateList(l);
                        }
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                System.out.println(new String(responseBody));
                }
            });
        }else{
            EgeneApi.getHealthListByType(userDao.getUser().getUserID(), type, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                    ResponseList<HealthListItem> response = JsonUtil.toList(responseBody,
                            new TypeToken<ResponseList<HealthListItem>>() {
                            }.getType());
                    if (response != null) {
//                    System.out.println(response);
                        updateList(response.getData());
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                System.out.println(new String(responseBody));
                }
            });
        }

    }

    private List<HealthListItem> tr(List<HealthItem> list){
        List<HealthListItem> listItems = new ArrayList<>();
        if(list == null){
            return listItems;
        }

        for(HealthItem item : list){
            HealthListItem listItem = new HealthListItem();
            listItem.setRid(item.getRid());
            listItem.setTitle(item.getTitle());
            listItem.setZhaiyao(item.getCareremark());
            listItem.setOrderId(""+item.getOrderid());
            listItem.setImg_url(item.getSmallimg());
            listItem.setNoImage(true);
            listItems.add(listItem);
        }

        return listItems;
    }


    @Override
    public void loadMore() {
//        super.loadMore();
    }

    @UiThread
    void updateList(List<HealthListItem> list) {
        if (mCurrentPage == START_PAGE_INDEX) {
            mAdapter.clear();
        }
        mAdapter.addData(list);
        mAdapter.notifyDataSetChanged();
        executeOnLoadFinish();
    }

    @Override
    protected void onItemClick(View view, int position) {
        HealthListItem item = (HealthListItem) mAdapter.getItem(position);

        if(item.isNoImage()){
            MoreDetailActivity_.intent(getActivity()).rid(item.getRid()).orderId(item.getOrderId()).start();
        }else {
            Intent intent = new Intent(getActivity(), WikInfoActivity_.class);
            intent.putExtra(WikInfoActivity.ID, "" + item.getId());
            startActivity(intent);
        }
//        Intent intent = new Intent(getActivity(), CategoryInfoActivity_.class);
//        intent.putExtra(CategoryInfoActivity.ID, "" + category.getId());
//        startActivity(intent);
    }
}
